package com.example.petcare.controller.user;

import com.example.petcare.pojo.*;
import com.example.petcare.service.GoodsService;
import com.example.petcare.service.HostService;
import com.example.petcare.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController//标记为控制器类,并且返回json数据
@RequestMapping("/user/host")//请求路径
@Slf4j
public class HostController {

    @Autowired
    private OrderService orderservice;

    @Autowired
    private HostService hostService;




    // 发布托管需求
    @PostMapping
    private Result add(@RequestBody HostDemand hostDemand) {
        hostService.add(hostDemand);
        return Result.success();
    }

    // 查询托管需求列表（条件分页查询）
    @GetMapping
    public Result<PageBean<HostDemand>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer petId,
            @RequestParam(required = false) String location,
            @RequestParam(required = false) Integer status)
    {

        PageBean<HostDemand> pb= hostService.list(pageNum, pageSize, petId, location, status);
        return Result.success(pb);

    }

    // 修改托管需求
    @PutMapping
    public Result update(@RequestBody HostDemand hostDemand) {
        hostService.update(hostDemand);
        return Result.success();
    }

    // 删除托管需求
    @DeleteMapping
    public Result delete(Integer hostId) {
        hostService.delete(hostId);
        return Result.success();
    }

    // 查询所有已经审核过的商家的具体信息，从provider表中查询
    @GetMapping("/provider")
    public Result<List<Provider>> listProvider() {
        List<Provider> list = hostService.listProvider();
        return Result.success(list);
    }


    // 删除已经被拒单的托管需求，利用userId删除，userId从threadLocal中获取
    @DeleteMapping("/reject")
    public Result deleteByUserId() {
        hostService.deleteByUserId();
        return Result.success();
    }


    // 查询当前用户被拒单的托管需求
    @GetMapping("/reject")
    public Result<List<HostDemand>> listReject() {
        List<HostDemand> list = hostService.listReject();
        return Result.success(list);
    }


    // 查询是否有订单被接单
    @GetMapping("/accept")
    public Result<Integer> hasAccept() {
        Integer hasAccept = hostService.hasAccept();
        return Result.success(hasAccept);
    }

    // 查询是否有订单被完成
    @GetMapping("/complete")
    public Result<Integer> hasFinish() {
        Integer hasFinish = hostService.hasFinish();
        return Result.success(hasFinish);
    }

    // 完成订单，更新订单状态（orderStatus）
    @PutMapping("/complete")
    public Result updateOrderStatus(@RequestParam("hostId") Integer hostId) {
        orderservice.updateOrderStatus(hostId);
        return Result.success();

    }


    // 查看增值商品列表
    @GetMapping("/getGoodsList")
    public Result<List<Goods>> GoodsList(@RequestParam("hostId") Integer hostId) {

        List<Goods> glist = hostService.getGoodsList(hostId);

        return Result.success(glist);
    }

    // 根据providerId查询商家信息
    @GetMapping("/getProvider")
    public Result<Provider> getProviderById(@RequestParam("hostId") Integer hostId) {
        Provider provider = hostService.getProviderById(hostId);
        return Result.success(provider);
    }

}
